import java.util.ArrayList;  
import java.util.List;
import java.util.Scanner;

public class BinaryTreeTraversal {  
  
    // 定义二叉树节点  
    static class TreeNode {  
        int val;  
        TreeNode left;  
        TreeNode right;  
  
        TreeNode(int x) {  
            val = x;  
        }  
    }  
  
    // 递归函数，根据后序遍历和中序遍历结果重建二叉树  
    private static TreeNode buildTree(int[] postorder, int postStart, int postEnd, int[] inorder, int inStart, int inEnd) {  
        if (postStart > postEnd || inStart > inEnd) {  
            return null;  
        }  
  
        // 后序遍历的最后一个节点是根节点  
        int rootVal = postorder[postEnd];  
        TreeNode root = new TreeNode(rootVal);  
  
        // 在中序遍历中找到根节点的索引  
        int rootIndex = -1;  
        for (int i = inStart; i <= inEnd; i++) {  
            if (inorder[i] == rootVal) {  
                rootIndex = i;  
                break;  
            }  
        }  
  
        // 递归构建左子树和右子树  
        int leftSubtreeSize = rootIndex - inStart;  
        root.left = buildTree(postorder, postStart, postStart + leftSubtreeSize - 1, inorder, inStart, rootIndex - 1);  
        root.right = buildTree(postorder, postStart + leftSubtreeSize, postEnd - 1, inorder, rootIndex + 1, inEnd);  
  
        return root;  
    }  
  
    // 前序遍历二叉树  
    private static void preOrderTraversal(TreeNode root, List<Integer> result) {  
        if (root == null) {  
            return;  
        }  
        result.add(root.val);  
        preOrderTraversal(root.left, result);  
        preOrderTraversal(root.right, result);  
    }  
  
    // 主函数，接收后序遍历和中序遍历数组，返回前序遍历结果  
    public static List<Integer> preorderTraversalFromPostIn(int[] postorder, int[] inorder) {  
        List<Integer> result = new ArrayList<>();  
        TreeNode root = buildTree(postorder, 0, postorder.length - 1, inorder, 0, inorder.length - 1);  
        preOrderTraversal(root, result);  
        return result;  
    }  
  
    // 测试  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] postorder= new int[n];
        int[] inorder = new int[n];
        for (int i = 0; i < n; i++) {
            postorder[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            inorder[i] = sc.nextInt();
        }
        List<Integer> preorder = preorderTraversalFromPostIn(postorder, inorder);
      for(Integer i : preorder) {
          System.out.print(i+" ");
      }
    }  
}